from PyQt5.QtWidgets import QDialog
from ui_personal_items import Ui_PersonalItems
from modifyPersonalInfo import ModifyPersonalInfoWindow
import requests
from network import Network


class PersonalItemsWindow(QDialog, Ui_PersonalItems):
    def __init__(self, username, parent=None):
        super(PersonalItemsWindow, self).__init__(parent=parent)
        self.setupUi(self)
        self.username = username
        self.modifyButton.clicked.connect(self.modify)
        self.searchButton.clicked.connect(self.search)
        self.personalGoodsList.itemClicked.connect(self.info)
        self.personal_info = Network.find_user_info(self.username)

        self.show_personal_info()

    def modify(self):
        print("modify")
        self.ModifyInfo = ModifyPersonalInfoWindow(self.username)
        self.close()
        self.ModifyInfo.show()

    def search(self):
        self.personalGoodsList.clear()
        goodsList = Network.getGoods_username(self.username)
        self.personalGoodsList.goodsList = goodsList
        for goods in goodsList:
            self.personalGoodsList.addItem(goods["name"])

    def info(self, item):
        self.label_phone_owner.setText("拥有者")
        self.label_addr_status.setText("状态")
        self.label_name_type.setText("类型")
        self.label_password_describe.setText("描述")
        for goods in self.personalGoodsList.goodsList:
            if goods['name'] == item.text():
                self.nameLabel_owner.setText(goods['name'])
                self.addrLabel_type.setText(goods['goodsType'])
                self.phoneLabel_status.setText(goods['status'])
                self.passwordLabel_describe.setText(goods['describe'])

    def show_personal_info(self):
        print(self.personal_info)
        self.nameLabel_owner.setText(self.personal_info["username"])
        self.passwordLabel_describe.setText(self.personal_info["password"])
        self.addrLabel_type.setText(self.personal_info["addr"])
        self.phoneLabel_status.setText(self.personal_info["phone"])
